﻿using QuanLyGiaoVuDAL.ADOWrapper;
using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuDAL.Mapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuDAL.Repositories.DatabaseRepositories
{
    public class DangKyKetThucLopMonHocRepository:IDangKyKetThucLopMonHocRepository
    {
        public IEnumerable<DangKyKetThucLopMonHocDTO> GetAllDangKy()
        {
            List<DangKyKetThucLopMonHocDTO> listDangKy = new List<DangKyKetThucLopMonHocDTO>();
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = DBParameterFactory.Create("GiangVienID", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[1] = DBParameterFactory.Create("MaHocKy", SqlDbType.UniqueIdentifier, DBNull.Value);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_DangKyKetThucLopMonHocSelect]", parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    listDangKy.Add(dataRow.ToDangKyKetThucLopMonHocDTO());
                }
                return listDangKy.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public IEnumerable<DangKyKetThucLopMonHocDTO> GetAllDangKyByGiangVien(Guid GiangVienID, Guid MaHocKy)
        {
            List<DangKyKetThucLopMonHocDTO> listDangKy = new List<DangKyKetThucLopMonHocDTO>();
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = DBParameterFactory.Create("GiangVienID", GiangVienID);
            parameters[1] = DBParameterFactory.Create("MaHocKy", MaHocKy);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_DangKyKetThucLopMonHocSelect]", parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    listDangKy.Add(dataRow.ToDangKyKetThucLopMonHocDTO());
                }
                return listDangKy.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public DangKyKetThucLopMonHocDTO InsertDangKy(DangKyKetThucLopMonHocDTO dangky)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_DangKyKetThucLopMonHocInsert]", GetParameterArray(dangky));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToDangKyKetThucLopMonHoc2DTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public DangKyKetThucLopMonHocDTO UpdateDangKy(DangKyKetThucLopMonHocDTO dangky)
        {

            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_DangKyKetThucLopMonHocUpdate]", GetParameterArray(dangky));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToDangKyKetThucLopMonHoc2DTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool DeleteDangKy(Guid ID)
        {            
            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_DangKyKetThucLopMonHocDelete]", DBParameterFactory.Create("@ID", ID));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #region Private Methods
        private System.Data.SqlClient.SqlParameter[] GetParameterArray(DangKyKetThucLopMonHocDTO dangky)
        {
            SqlParameter[] parameters = new SqlParameter[6];
            parameters[0] = DBParameterFactory.Create("ID", dangky.ID);
            parameters[1] = DBParameterFactory.Create("GiangVienID", dangky.GiangVienID);
            parameters[2] = DBParameterFactory.Create("MaLopMonHoc", dangky.MaLopMonHoc);
            parameters[3] = DBParameterFactory.Create("NgayKetThucLopMonHoc", dangky.NgayKetThucLopMonHoc);
            parameters[4] = DBParameterFactory.Create("MaHocKy", dangky.MaHocKy);
            parameters[5] = DBParameterFactory.Create("NgayDangKy", dangky.NgayDangKy);

            return parameters;
        }
        #endregion
    }
}
